/*
 * @Author: 毅凡
 * @Date: 2020-10-20 09:45:58
 * @LastEditTime: 2020-10-24 10:09:58
 * @LastEditors: 毅凡
 */
var AES = require('crypto-js/aes');
var HmacMD5 = require('crypto-js/hmac-md5');
var CryptoJs = require('crypto-js');
// #ifdef H5
var store = require('storejs').default;
// #endif
/* 
  uni react vue 都可用
*/
class Auth {
  constructor() {
    this.get = uni ? uni.getStorageSync : store.get;
    this.set = uni ? uni.setStorageSync : store.set;
    this.remove = uni ? uni.removeStorageSync : store.remove;
  }
 getAuth(url, username, password) {
   // password = password + '';
   if (!password && !this.get('user')) {
     return null;
   }
 
   var iv = CryptoJs.enc.Latin1.parse('O2%=!ExPCuY6SKX(');
   var key = CryptoJs.enc.Latin1.parse(
     password
       ? HmacMD5(password, password).toString()
       : this.get('user').password
   );
   var pass = AES.encrypt(url + ':' + new Date().getTime(), key, {
     iv: iv,
     mode: CryptoJs.mode.CBC,
     padding: CryptoJs.pad.ZeroPadding
   }).toString();
   return (username ? username : this.get('user').username) + ':' + pass;
   // return (username ? username : this.get('user').username) + ':' + pass ;
 }
  logIn(username,password) {
    this.set('user', {
      username:username,
      password: HmacMD5(password, password).toString(),
    });
  }
  logOut() {
    this.remove('user');
  }
  isLogin() {
    let user = this.get('user');
    if (!!user) {
      return user;
    } else {
      return false;
    }
  }
}

export default new Auth();
